package sequencial;

import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import java.util.*;

public class CircClient extends JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final int IMG_SIZE = 256;
    byte[][] pic = new byte[IMG_SIZE][IMG_SIZE];
    int[] lut = new int[256];

    CircServer myServer;

    BufferedImage img = new BufferedImage(IMG_SIZE, IMG_SIZE,BufferedImage.TYPE_INT_BGR);
 
    public void start() {
        setTitle("Circ");
        setSize(IMG_SIZE,IMG_SIZE+30);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        // criar um mapa de cores (tons de cinzento)
        for (int i=0; i<128; i++) lut[i]=2*i+256*i*2+256*256*i*2;
     
        Image im = getImage("im2.jpg");
        int[] pixels = new int[IMG_SIZE * IMG_SIZE];

        // converts Image to array of byte
        PixelGrabber pg = new PixelGrabber(im, 0, 0, IMG_SIZE, IMG_SIZE, pixels, 0, IMG_SIZE); 
        try { 
            pg.grabPixels();   
        } catch (InterruptedException ee) {
            System.err.println("interrupted waiting for pixels!");
             return;
        }
        for(int i=0; i<IMG_SIZE; i++)
           for(int j=0; j<IMG_SIZE; j++)
                if ((pixels[j*IMG_SIZE + i]&0x80)==0x80) pic[i][j]=127;
                else pic[i][j]=0;
        
        myServer = new CircServer();
            
        repaint();
        double start = new Date().getTime();
        pic = myServer.circles(pic,IMG_SIZE);
        System.out.println("Demorou " + (new Date().getTime()-start) + " ms");
        repaint();
      
    }

   
    public void paint(Graphics g) { 
 
        for(int i=0; i<IMG_SIZE; i++)
           for(int j=0; j<IMG_SIZE; j++) {
               //System.out.print(pic[i][j] + " ");
               img.setRGB(i,j,lut[pic[i][j]]);
            }
       Graphics2D g2 = (Graphics2D) g;
       g2.drawImage(img,0,25,this);   
    }
 
    protected Image getImage(String fileName) {
        Image img = getToolkit().getImage(fileName);
        try {
           MediaTracker tracker = new MediaTracker(this);
           tracker.addImage(img, 0);
           tracker.waitForID(0);
           tracker.removeImage(img,0);
        } catch (Exception e) { e.printStackTrace(); }
        return img;
    }

    public static void main(String[] arg) {
        CircClient mc = new CircClient();
        mc.start();
    }
}
